

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_pleasure_video/api/dou_ban_api.dart';
import 'package:flutter_pleasure_video/constant/color.dart';
import 'package:flutter_pleasure_video/data/model/video_model.dart';
import 'package:flutter_pleasure_video/widget/common/app_icon_button.dart';
import 'package:flutter_pleasure_video/widget/common/common_widget.dart';
import 'package:flutter_pleasure_video/util/extensions/string_extensions.dart';
import 'package:flutter_pleasure_video/widget/slider/complicated_image_slider.dart';


class TabHomePage extends StatefulWidget {

  final Function(Function(bool isPlay)) isPlayListener;

  TabHomePage(this.isPlayListener);

  @override
  _TabHomePageState createState() => _TabHomePageState();
}

class _TabHomePageState extends State<TabHomePage> with AutomaticKeepAliveClientMixin{

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      color: AppColor.white,
      child: Column(
        children: [
          _getTopSearchBar(),
          SizedBox(height: 12),
          // _getVideoBanner(),
        ],
      ),
    );
  }

  //顶部搜索
  Widget _getTopSearchBar(){
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        children: [
          CommonWidget.init().getUserImage(null, 39),
          _getSearchBox(),
          AppIconButton(
            iconSize: 27,
            icon: Icons.history_outlined,
            iconColor:AppColor.black.withOpacity(0.8),
            buttonHeight: 32,
            buttonWidth: 32,
          ),
        ],
      ),
    );
  }

  Widget _getSearchBox(){
    return Expanded(child: SizedBox(
      child: Container(
        height: 32,
        margin: EdgeInsets.symmetric(horizontal: 16),
        padding: EdgeInsets.only(left: 10),
        decoration: BoxDecoration(
          color: AppColor.bgWhite,
          borderRadius: BorderRadius.circular(16),
        ),
        alignment: Alignment.centerLeft,
        child: AppIconButton(
          iconSize: 20,
          icon: Icons.search_outlined,
          iconColor:AppColor.black.withOpacity(0.2),
        ),
      ),
    ));
  }


  Widget _getVideoBanner(){
    return FutureBuilder(
        future: DouBanApi.init().getDouBanVideoListNumber(number: 5),
        builder: (BuildContext context, AsyncSnapshot<List<VideoModel>?> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              return  Container(child: Text(snapshot.error.toString()));
            }
            if (snapshot.data==null||snapshot.data!.length<1) {
              return  Container(child: Text("暂无数据"));
            }
            return  Container(child: ComplicatedImageSlider(snapshot.data,_onClickBannerListener,widget.isPlayListener));
          }
          return Container(child: Text("正在加载"));
        }
    );
  }

  //banner位的点击事件
  _onClickBannerListener(VideoModel model){
    "model:${model.toJson().toString()}".print;
  }


}

